package com.luhongda.music.common;

import cn.hutool.core.io.StreamProgress;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;

import java.io.File;

public class MusicDownloadUtil {

    public static long downloadFile(String url, File destFile, StreamProgress streamProgress) {
        if (StrUtil.isBlank(url)) {
            throw new NullPointerException("[url] is null!");
        } else if (null == destFile) {
            throw new NullPointerException("[destFile] is null!");
        } else {
            MyHttpResponse response = (MyHttpResponse) HttpRequest.get(url).timeout(-1).executeAsync();
            if (!response.isOk()) {
                throw new HttpException("Server response error with status code: [{}]", response.getStatus());
            } else {
                return response.writeBody(destFile, streamProgress);
            }
        }
    }


}
